/*
 * ExtensionDAO.java
 *
 * Created on February 18, 2007, 8:31 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package edu.byu.isys413.samlam;

import java.sql.*;
import java.util.LinkedList;
import java.util.List;

/**
 * Extension DAO
 * @author Sam
 */
public class ExtensionDAO {
    ///////////////////////////////////////
    ///   Singleton pattern
    
    private static ExtensionDAO instance;
    
    /** Creates a new instance of ExtensionDAO */
    private ExtensionDAO() {
    }

    
    
    /**
     * get instance
     * @return get instance
     */
    public static synchronized ExtensionDAO getInstance() {
        if (instance == null) {
            instance = new ExtensionDAO();
        }
        return instance;
    }
    
    /////////////////////////////////
    ///   CREATE
    // we are not supporting CREATE because this is a useless
    // DAO anyway.
    /**
     * There's no need for two creates because we don't need
     * a connection to create BOs.  We run the insert statement
     * later, when it get's saved for the first time.
     */

    
    
    /////////////////////////////////////
    ///   READ
    
    /**
     * This is the public read statement.  It loads an existing record
     * from the database.
     * @param fileType file type
     * @throws edu.byu.isys413.samlam.DataException data exception
     * @return read
     */
    public synchronized Extension read(String fileType) throws DataException {
        // check to see if id in the cache
        Extension extension = (Extension)Cache.getInstance().get(fileType);
        if(extension != null){
            // if so, return it immediately
            return extension;
        }
        
        
        
        Connection conn = null;
        try {
            // retrieve a database connection from the pool
            conn = ConnectionPool.getInstance().get();
            // call read with a connection (the other read method in this class)
            extension = ExtensionDAO.getInstance().read(fileType, conn);
            
            // release the connection
            conn.commit();
            ConnectionPool.getInstance().release(conn);
            
        }catch (ConnectionPoolException e4){
            throw new DataException("Could not get a connection to the database.");
            
        }catch (SQLException e) {
            // rollback
            try{
                conn.rollback();
                ConnectionPool.getInstance().release(conn);
            }catch(SQLException e2){
                throw new DataException("Big error: could not even release the connection", e2);
            }catch (ConnectionPoolException e3){
                throw new DataException("Could not get a connection to the database.");
                
            }
            throw new DataException("Could not retrieve record for id=" + fileType, e);
        }
        
        // return object
        return extension;
    }
    
    /**
     *  This is a package method that is called by the public read (above) or
     *  by another DAO.  Either way we already have a connection to the database
     *  to use.  The user (controller) code never calls this one directly, since
     *  it can't know about Connection objects or SQLExceptions.
     */
    synchronized Extension read(String id, Connection conn) throws SQLException, DataException {
        // check again if the id is in the cache, and if so,
        // just get it from the cache.  we need to check again
        // because this method might be called directly from
        // another DAO rather than from read above.
        Extension backupitem = (Extension)Cache.getInstance().get(id);
        if(backupitem != null){
            return backupitem;
        }
        // if not in the cache, get a result set from
        // a SELECT * FROM table WHERE id=guid
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery("SELECT * FROM icon WHERE filetype = '"+ id + "'");
        
        //create a prepared statement
        //query the database
        //call sets from the result set
        Extension extension = null;
        String memberId=null;
        if (rs.next()) {
            extension = new Extension();
            
            
            extension.setFilepath(rs.getString("filepath"));
            extension.setFileType(rs.getString("filetype"));
                       
            // get any subobjects (for example, if this was the ExtensionDAO,
            // we'd need to call the MembershipDAO to load the membership record.

            
            
            // save to the cache
            
            Cache.getInstance().put(extension.getFileType(), extension);
            stmt.close();
        }else{
            rs = stmt.executeQuery("SELECT * FROM icon WHERE filetype = 'unknown'");
            
            if(rs.next()){
            extension = new Extension();
            
            
            extension.setFilepath(rs.getString("filepath"));
            extension.setFileType(rs.getString("filetype"));
                       
            
            Cache.getInstance().put(extension.getFileType(), extension);
            stmt.close();
            }else{
                
                System.out.println("noting in database");
            }
            
        }
        // return the BO
        return extension;
    }
    
    //////////////////////////////////
    ///   UPDATE
    
    /**
     * This is the public save method.  It is what is called when
     * the user (controller) code wants to save or update an object
     * into the database.
     */
   //there will be no update.
    
   

}
